import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';

let adminMenus = JSON.parse(localStorage.getItem('adminMenus') + "");
if (adminMenus == null) {
  adminMenus = [];
}

const routeList: { path: any; name: any; component: () => Promise<any>; meta: { title: any; }; }[] = [];
adminMenus.forEach((item: any) => {
  routeList.push({
    path: item.path,
    name: item.code,
    component: () => import(`../views/${item.component}`),
    meta: {
      title: item.name
    }
  });
});

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/Body.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/manage',
    name: 'manage',
    component: () => import('../views/manage/Body.vue'),
    meta: {
      title: '后台管理'
    },
    children: [{
      path: '/manage/index',
      name: 'index',
      component: () => import('../views/manage/pages/Index/Index.vue'),
      meta: {
        title: '首页'
      }
    },{
      path: '/manage/goods/goodsType',
      name: 'GoodsType',
      component: () => import('../views/manage/pages/Goods/GoodsType/Body.vue'),
      meta: {
        title: '商品分类'
      }
    },{
      path: '/manage/goods/goods',
      name: 'Goods',
      component: () => import('../views/manage/pages/Goods/Goods/Body.vue'),
      meta: {
        title: '商品'
      }
    },{
      path: '/manage/goodsStore',
      name: 'GoodsStore',
      component: () => import('../views/manage/pages/GoodsStore/Body.vue'),
      meta: {
        title: '门店与店员'
      }
    },{
      path: '/manage/goodsWarehouse/goodsWarehouse',
      name: 'GoodsWarehouse',
      component: () => import('../views/manage/pages/GoodsWarehouse/GoodsWarehouse/Body.vue'),
      meta: {
        title: '仓库管理'
      }
    },
    {
      path: '/manage/goodsWarehouse/goodsWarehouseSearch',
      name: 'GoodsWarehouseSearch',
      component: () => import('../views/manage/pages/GoodsWarehouse/GoodsWarehouseSearch/Body.vue'),
      meta: {
        title: '仓库查询'
      }
    },
    {
      path: '/manage/goodsWarehouse/goodsWarehouseDetail',
      name: 'GoodsWarehouseDetail',
      component: () => import('../views/manage/pages/GoodsWarehouse/GoodsWarehouseDetail/Body.vue'),
      meta: {
        title: '仓库流水'
      }
    },
    // 商品采购/商品供应商
    {
      path: '/manage/GoodsProcure/goodsSupplier',
      name: 'GoodsSupplier',
      component: () => import('../views/manage/pages/GoodsProcure/GoodsSupplier/Body.vue'),
      meta: {
        title: '商品供应商'
      }
    },
    // GoodsProcureOrder
    {
      path: '/manage/GoodsProcure/goodsProcureOrder',
      name: 'GoodsProcureOrder',
      component: () => import('../views/manage/pages/GoodsProcure/GoodsProcureOrder/Body.vue'),
      meta: {
        title: '商品采购单'
      }
    },
  ]
  }
]


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


router.beforeEach((to, from, next) => {
  document.title = to.meta.title + "";
  next();
})

export default router
